//
//  JHNavigationController.swift
//  Weibo_SwiftDemo
//
//  Created by gjh on 2020/12/21.
//

import UIKit

class JHNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationBar.isHidden = true
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        // 如果不是栈底控制器才需要隐藏,跟控制器不需要处理
        if viewControllers.count > 0 {
            // 隐藏底部的 tabbar
//            viewController.hidesBottomBarWhenPushed = true            
            if let vc = viewController as? JHBaseViewController {
                
                var title = "返回"
                // 判断控制器的级数,只有一个子控制器的时候显示栈底控制器的标题
                if viewControllers.count == 1 {
                    title = viewControllers.first?.title ?? "返回"
                }
                
                vc.naviItem.leftBarButtonItem = UIBarButtonItem(title: title, titleColor: UIColor.darkGray, highlightColor: UIColor.orange, target: self, action: #selector(popToBack), isback: true)
            }
        }
        super.pushViewController(viewController, animated: true)
    }
    
    @objc private func popToBack() {
        popViewController(animated: true)
    }

}
